package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.weizhi.rjwm.entity.User;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.UserService;
import xyz.weizhi.rjwm.utils.SMSUtils;
import xyz.weizhi.rjwm.utils.ValidateCodeUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author milian
 * @Date 2021/9/28 0028 19:59
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/sendSMS")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();
        if (StringUtils.isNotBlank(phone)) {
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            String[] phones={"+86"+phone};
            //SMSUtils.sendSms(phones,code.toString());
            log.info("code:{}",code);
            //session.setAttribute(phone,code.toString());
            redisTemplate.opsForValue().set(phone,code.toString(),5, TimeUnit.MINUTES);
            return R.success("手机验证码短信发送成功");
        }
        return R.error("短信发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //Object codeSession = session.getAttribute(phone);
        String codeSession = redisTemplate.opsForValue().get(phone).toString();
        if (codeSession!=null&&codeSession.equals(code)){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,phone);
            User user = userService.getOne(lqw);
            //判断是否存在用户，如果不存在则注册用户
            if (user==null){
                User newUser = new User();
                newUser.setPhone(phone);
                newUser.setStatus(1);
                userService.save(newUser);
                user=newUser;
            }
            session.setAttribute("user",user.getId());
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
    }
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("登出成功");
    }
    }
